#!/bin/bash
# howto 36   # builds for 3.6
# howto 37 36 27 # builds for 3.7 3.6 and 2.7
#see nrn/howtomingw

set -e
set -x
# in cygwin terminal in neuron/nrn
#this part is not necessary with the MSYS2 installation
if test "x$OSTYPE" = "xcygwin" ; then

cd /cygdrive/f/msys64/home/hines/neuron/nrn
sh git2nrnversion_h.sh > src/nrnoc/nrnversion.h

cat src/nrnoc/neuron.h > tmp.c
cpp -Isrc/nrnoc -Isrc/oc tmp.c | sed '/^#/d' > neuron.tmp
python src/oc/mk_hocusr_h.py < neuron.tmp > temp2hoc
cat neuron.tmp temp2hoc |sed 's/"nrnhoc_topology"/"topology"/' > src/nrnoc/hocusr.h
rm -f temp1hoc temp2hoc neuron.tmp tmp.c

fi

# in msys terminal in nrnwinobj
if test "x$OSTYPE" = "xmsys" ; then

if false ; then
# howto build InterViews
cd ~/neuron/iv
./configure --prefix=`pwd` --host=x86_64-w64-mingw32
make
make install
fi

cd ~/neuron/nrnwinobj

#comment out if you do not have vcenv.sh. In that case there may be
#problems in building the dlls from cython generated cpp files in
# nrn/share/lib/python/neuron/rxd/geometry3d under python3.5
. ../vcenv.sh  #visual c environment allows cl for python (rxd for 35)

PYVS="py"

export CYTHON=cython

bld () {
PYVER=$1
PYVS=${PYVS}-${PYVER}
PYDIR=/e/python${PYVER}

rm -r -f src/nrnpython/build

../nrn/configure --prefix=`pwd` -with-nrnpython=dynamic \
 --with-pyexe=$PYDIR/python \
 --host=x86_64-w64-mingw32 PYINCDIR="$PYDIR/include" \
 --with-paranrn=dynamic MPICC=x86_64-w64-mingw32-gcc \
 MPICXX=x86_64-w64-mingw32-g++ \
 CFLAGS='-I/c/ms-mpi/include -O2' \
 CXXFLAGS='-I/c/ms-mpi/include -O2' \
 LIBS='-L/c/ms-mpi/lib/x64 -lmsmpi' \
  PYINCDIR=$PYDIR/include \
  PYLIBDIR=$PYDIR/libs \
  PYLIB="-L$PYDIR/libs -lpython${PYVER}" \
  PYLIBLINK="-L$PYDIR/libs -lpython${PYVER}" \
  MPILIBS='-L/c/ms-mpi/lib/x64 -lmsmpi' # --disable-maintainer-mode

make
}

bld $1 ""
shift
for i in $* ; do
  bld $i "--with-nrnpython-only"
done

ver="`sh ../nrn/nrnversion.sh 3`"
n1=nrn"${ver}"setup-x86_64.exe
n2=nrn-"`sh ../nrn/nrnversion.sh describe`".w64-mingw-${PYVS}-setup.exe
rm -f src/mswin/$n1
make mswin

if false ; then
sh ../cpmxe.sh
fi

echo "scp src/mswin/$n1  hines@neuron.yale.edu:/home/htdocs/ftp/neuron/versions/alpha/$n2"

h=hines@10.10.0.2
scp src/mswin/$n1 $h:vmshared/$n2
if true ; then
ssh $h scp vmshared/$n2 hines@neuron.yale.edu:/home/htdocs/ftp/neuron/versions/alpha/$n2
fi

fi #MSYS

